package com.amigo.online.provider.adv.controller;

import java.util.List;
import java.util.Map;

import com.amigo.online.provider.adv.feign.UserFeignService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.core.security.IgnoreSecurity;
import com.amigo.online.common.param.dto.adv.AdvClickDto;
import com.amigo.online.common.param.dto.adv.AdvHomeDto;
import com.amigo.online.common.param.dto.adv.AdvLookDto;
import com.amigo.online.common.param.dto.adv.AdvNearbyDto;
import com.amigo.online.common.param.dto.adv.AdvRecommendDto;
import com.amigo.online.common.param.dto.adv.AdvTantanDto;
import com.amigo.online.common.param.dto.video.AdvApplyDto;
import com.amigo.online.common.param.dto.video.AdvSourceCheckDto;
import com.amigo.online.common.param.dto.video.VideoDto;
import com.amigo.online.common.param.result.AdvInfo;
import com.amigo.online.common.util.CommonValidator;
import com.amigo.online.provider.adv.aspect.RequestHeaderEntity;
import com.amigo.online.provider.adv.entity.AdvRequestHeader;
import com.amigo.online.provider.adv.exception.AdvException;
import com.amigo.online.provider.adv.service.AdvHomeStockService;
import com.amigo.online.provider.adv.service.AdvNearbyStockService;
import com.amigo.online.provider.adv.service.AdvRecommendStockService;
import com.amigo.online.provider.adv.service.AdvService;
import com.amigo.online.provider.adv.service.AdvTantanStockService;
import com.amigo.online.provider.adv.service.AreaService;
import com.amigo.online.provider.adv.service.HomeAdvService;
import com.amigo.online.provider.adv.service.TantanAdvService;

/**
 * @version: V 1.0.0
 * @Description: （广告统一接口）
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月3日 上午9:29:32
 */
@RestController
@RequestMapping(value = "/v1/adv", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class AdvController extends BaseController {

    @Autowired
    AreaService areaService;

    @Autowired
    AdvService advService;

    @Autowired
    TantanAdvService tantanAdvService;

    @Autowired
    HomeAdvService homeAdvService;

    @Autowired
    AdvHomeStockService advHomeStockService;

    @Autowired
    AdvTantanStockService advTantanStockService;

    @Autowired
    AdvNearbyStockService advNearbyStockService;

    @Autowired
    AdvRecommendStockService advRecommendStockService;

    @Autowired
    UserFeignService userFeignService;

    /**
     * 获取探探广告
     *
     * @return
     */
    @IgnoreSecurity
    @PostMapping(value = "/list/tantan")
    @RequestHeaderEntity(target = "AdvRequestHeader")
    public ResponseEntity<Response> getTantanAdvSource(@RequestBody AdvTantanDto advTantanDto, AdvRequestHeader advRequestHeader) {
        if (advTantanDto.getNumber() <= 0 || advTantanDto.getNumber() > 3) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        Double lng = null, lat = null;
        try {
            if (advRequestHeader.getLng() != null && advRequestHeader.getLat() != null) {
                lng = Double.parseDouble(advRequestHeader.getLng());
                lat = Double.parseDouble(advRequestHeader.getLat());
            }
        } catch (NumberFormatException e) {
            logger.error("经纬度转换错误");
        }
        List<AdvInfo> result = advService.getAdvList(advTantanDto.getCity(), lng, lat,
                advTantanDto.getSex(), advTantanDto.getAge(), advRequestHeader.getLanguage(), advTantanDto.getWidth(),
                advTantanDto.getHeight(), advTantanDto.getNumber(), 4);

        return success(result);
    }

    /**
     * 获取附近的人广告
     *
     * @return
     */
    @IgnoreSecurity
    @PostMapping(value = "/list/nearby")
    @RequestHeaderEntity(target = "AdvRequestHeader")
    public ResponseEntity<Response> getNearbyAdvSource(@RequestBody AdvNearbyDto advNearbyDto, AdvRequestHeader advRequestHeader) {
        if (advNearbyDto.getNumber() <= 0 || advNearbyDto.getNumber() > 3) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        Double lng = null, lat = null;
        try {
            if (advRequestHeader.getLng() != null && advRequestHeader.getLat() != null) {
                lng = Double.parseDouble(advRequestHeader.getLng());
                lat = Double.parseDouble(advRequestHeader.getLat());
            }

        } catch (NumberFormatException e) {
            logger.error("经纬度转换错误");
        }
        List<AdvInfo> result = advService.getAdvList(advNearbyDto.getCity(), lng, lat, advNearbyDto.getSex(), advNearbyDto.getAge(),
                advRequestHeader.getLanguage(), advNearbyDto.getWidth(), advNearbyDto.getHeight(), advNearbyDto.getNumber(), 3);
        return success(result);
    }


    /**
     * 获取APP主页广告
     *
     * @return
     */
    @IgnoreSecurity
    @PostMapping(value = "/list/home")
    @RequestHeaderEntity(target = "AdvRequestHeader")
    public ResponseEntity<Response> getHomeAdvSource(@RequestBody AdvHomeDto advHomeDto, AdvRequestHeader advRequestHeader) {
        Double lng = null, lat = null;
        try {
            if (advRequestHeader.getLng() != null && advRequestHeader.getLat() != null) {
                lng = Double.parseDouble(advRequestHeader.getLng());
                lat = Double.parseDouble(advRequestHeader.getLat());
            }
        } catch (NumberFormatException e) {
            logger.error("经纬度转换错误");
        }
        List<AdvInfo> result = advService.getAdvList(advHomeDto.getCity(), lng, lat, advHomeDto.getSex(), advHomeDto.getAge(),
                advRequestHeader.getLanguage(), advHomeDto.getWidth(), advHomeDto.getHeight(), 1, 1);
        userFeignService.loginPath(advRequestHeader.getLng(),advRequestHeader.getLat());
        return success(result);
    }

    /**
     * 获取推荐视频广告
     *
     * @return
     */
    @IgnoreSecurity
    @PostMapping(value = "/list/recommended")
    @RequestHeaderEntity(target = "AdvRequestHeader")
    public ResponseEntity<Response> getRecommendVideoAdvSource(@RequestBody AdvRecommendDto advRecommendDto, AdvRequestHeader advRequestHeader) {
        if (advRecommendDto.getNumber() <= 0 || advRecommendDto.getNumber() > 3) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        Double lng = null, lat = null;
        try {
            if (advRequestHeader.getLng() != null && advRequestHeader.getLat() != null) {
                lng = Double.parseDouble(advRequestHeader.getLng());
                lat = Double.parseDouble(advRequestHeader.getLat());
            }
        } catch (NumberFormatException e) {
            logger.error("经纬度转换错误");
        }
        List<AdvInfo> result = advService.getAdvList(advRecommendDto.getCity(), lng, lat, advRecommendDto.getSex(), advRecommendDto.getAge(), advRequestHeader.getLanguage(), advRecommendDto.getWidth(), advRecommendDto.getHeight(), advRecommendDto.getNumber(), 2);
        return success(result);
    }

    /**
     * ,
     * 记录推荐视频广告观看记录
     *
     * @return
     */
    @IgnoreSecurity
    @PostMapping(value = "/look/recommended")
    public ResponseEntity<Response> recordLookRecommendVideoAdv(@RequestBody AdvLookDto advLookDto) {
        if (advLookDto.getAdvId() == null || advLookDto.getAdvId() <= 0) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        advRecommendStockService.recordLookRecommendAdv(advLookDto.getAdvId(), advLookDto.getDevice(), advLookDto.getUserId());
        return success(true);
    }

    /**
     * 记录探探广告观看记录
     *
     * @return
     */
    @PostMapping(value = "/look/tantan")
    @IgnoreSecurity
    public ResponseEntity<Response> recordLookTantanAdv(@RequestBody AdvLookDto advLookDto) {
        if (advLookDto.getAdvId() == null || advLookDto.getAdvId() <= 0) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        advTantanStockService.recordLookTantanAdv(advLookDto.getAdvId(), advLookDto.getDevice(), advLookDto.getUserId());
        return success(true);
    }

    /**
     * 记录附近的人广告观看记录
     *
     * @return
     */
    @IgnoreSecurity
    @PostMapping(value = "/look/nearby")
    public ResponseEntity<Response> recordLookNearbyAdv(@RequestBody AdvLookDto advLookDto) {
        if (advLookDto.getAdvId() == null || advLookDto.getAdvId() <= 0) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        advNearbyStockService.recordLookNearbyAdv(advLookDto.getAdvId(), advLookDto.getDevice(), advLookDto.getUserId());
        return success(true);
    }

    /**
     * 记录APP主页广告观看记录
     *
     * @return
     */
    @IgnoreSecurity
    @PostMapping(value = "/look/home")
    public ResponseEntity<Response> recordLookHomeAdv(@RequestBody AdvLookDto advLookDto) {
        if (advLookDto.getAdvId() == null || advLookDto.getAdvId() <= 0) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        advHomeStockService.recordLookHomeAdv(advLookDto.getAdvId(), advLookDto.getDevice(), advLookDto.getUserId());
        return success(true);
    }

    /**
     * 记录推荐视频广告观看记录
     *
     * @return
     */
    @IgnoreSecurity
    @PostMapping(value = "/click/recommended")
    public ResponseEntity<Response> recordClickRecommendVideoAdv(@RequestBody AdvClickDto advClickDto) {
        if (advClickDto.getAdvId() == null || advClickDto.getAdvId() <= 0) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        advRecommendStockService.recordClickRecommendAdv(advClickDto.getAdvId(), advClickDto.getDevice(), advClickDto.getUserId());
        return success(true);
    }

    /**
     * 记录探探广告观看记录
     *
     * @return
     */
    @PostMapping(value = "/click/tantan")
    @IgnoreSecurity
    public ResponseEntity<Response> recordClickTantanAdv(@RequestBody AdvClickDto advClickDto) {
        if (advClickDto.getAdvId() == null || advClickDto.getAdvId() <= 0) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        advTantanStockService.recordClickTantanAdv(advClickDto.getAdvId(), advClickDto.getDevice(), advClickDto.getUserId());
        return success(true);
    }

    /**
     * 记录附近的人广告观看记录
     *
     * @return
     */
    @IgnoreSecurity
    @PostMapping(value = "/click/nearby")
    public ResponseEntity<Response> recordClickNearbyAdv(@RequestBody AdvClickDto advClickDto) {
        if (advClickDto.getAdvId() == null || advClickDto.getAdvId() <= 0) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        advNearbyStockService.recordClickNearbyAdv(advClickDto.getAdvId(), advClickDto.getDevice(), advClickDto.getUserId());
        return success(true);
    }

    /**
     * 记录APP主页广告观看记录
     *
     * @return
     */
    @IgnoreSecurity
    @PostMapping(value = "/click/home")
    public ResponseEntity<Response> recordClickHomeAdv(@RequestBody AdvClickDto advClickDto) {
        if (advClickDto.getAdvId() == null || advClickDto.getAdvId() <= 0) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        advHomeStockService.recordClickHomeAdv(advClickDto.getAdvId(), advClickDto.getDevice(), advClickDto.getUserId());
        return success(true);
    }

    /**
     * @throws
     * @Title: getAreaIdByCity
     * @Description: (这里用一句话描述这个方法的作用)
     * @param: @param language
     * @param: @param city
     * @param: @return
     * @return: ResponseEntity<Response>
     */
    @PostMapping(value = "/area/areaid")
    @IgnoreSecurity
    public ResponseEntity<Response> getAreaIdByCity(@RequestHeader("Accept-Language") String language, @RequestBody Map<String, String> reuqestBody) {
        return success(areaService.getPositionAreaIdByCityName(reuqestBody.get("city"), language));
    }

    /**
     * @throws
     * @Title: getCommentList
     * @Description: (获取广告视频评论)
     * @param: @param videoId
     * @param: @param page
     * @param: @param size
     * @param: @return
     * @return: ResponseEntity<Response>
     */
    @GetMapping(value = "/comments/{videoId}/{page}/{size}")
    @IgnoreSecurity
    public ResponseEntity<Response> getCommentList(@PathVariable Long videoId, @PathVariable Integer page, @PathVariable Integer size) {
        if (videoId == null || videoId <= 0) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        //page = GlobalController.checkPage(page, size);
        //List<VideoComment> commentList = videoService.getComments(videoId,page,size);
        return success();
    }

    /**
     * 新增广告申请
     *
     * @param dto
     * @param adv
     * @return
     */
    @Deprecated
    @PostMapping(value = "/new")
    public ResponseEntity<Response> newUserVideo(@RequestBody AdvApplyDto adv) {
        if (!CommonValidator.validate(adv)) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        VideoDto dto = new VideoDto();
        BeanUtils.copyProperties(adv, dto);
        dto.setAds(1);
        //VideoInfo videoInfo = videoService.newUserVideo(dto);
        //boolean flag = advApplyService.newAdvApply(videoInfo, adv);
        return success(true);
    }

    /**
     * 检查是否可以申请，并计算总价格
     *
     * @param dto
     * @return
     */
    @Deprecated
    @PostMapping(value = "/check")
    public ResponseEntity<Response> checkSource(@RequestBody AdvSourceCheckDto dto) {
        if (!CommonValidator.validate(dto)) {
            throw new AdvException(StatusCode.BAD_REQUEST);
        }
        //Integer totalPrice = advApplyService.checkSource(dto);
        return success();
    }

    /**
     * 获取广告套餐
     *
     * @return
     */
    @Deprecated
    @GetMapping(value = "/bundle")
    public ResponseEntity<Response> getBundle() {
        return success();
    }


    /**
     * 获得地区列表
     *
     * @param countryId
     * @return
     */
    @GetMapping(value = "/areaInfo/{countryId}")
    public ResponseEntity<Response> getAreaInfo(@RequestHeader(name = "accept-language", required = false) String language, @PathVariable("countryId") Long countryId) {
        if (StringUtils.isBlank(language)) {
            language = "portuguese";
        }
        logger.info("获得的语言:" + language);
        return success(advService.getAreaInfoByCountryId(countryId, language));
    }
}
